package com.itheima.plusstudentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("------------欢迎来到黑马学生管理系统-------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    break loop;
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    public static void addStudent(ArrayList<Student> list) {
        //1.先创建一个学生对象
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生id");
            String id = sc.next();
            boolean flag = contains(list, id);
            if(flag){
                System.out.println("学生id已存在，请重新输入");
            }else{
                stu.setId(id);
                break;
            }
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        stu.setAddress(address);
        list.add(stu);
        System.out.println("添加学生信息成功");
    }

    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        int index = getIndex(list, id);
        if(index>=0){
            list.remove(index);
            System.out.println("id为"+id+"的学生信息删除成功");
        }else{
            System.out.println("id为"+id+"的学生信息删除不存在");
        }

    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index>=0){
            Student s = list.get(index);
            System.out.println("请输入要修改学生的姓名");
            String newName = sc.next();
            s.setName(newName);
            System.out.println("请输入要修改学生的年龄");
            int newAge = sc.nextInt();
            s.setAge(newAge);
            System.out.println("请输入要修改学生的家庭住址");
            String newAddress = sc.next();
            s.setAddress(newAddress);
        }else{
            System.out.println("id为"+id+"的学生信息不存在");
        }
    }

    public static void queryStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
        }else {
            System.out.println("id"+'\t'+'\t'+'\t'+"姓名"+'\t'+'\t'+"年龄"+'\t'+"家庭住址");
            for (int i = 0; i < list.size(); i++) {
                Student s = list.get(i);
                System.out.println(s.getId()+" "+'\t'+s.getName()+'\t'+s.getAge()+'\t'+s.getAddress());
            }
        }
    }
    public static boolean contains(ArrayList<Student> list, String id){
        return getIndex(list,id)>=0;
    }
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            String sId = s.getId();
            if (id.equals(sId)){
                return i;
            }
        }
        return -1;
    }
}
